#ParallelPlatform Project - A web platform to distribute and share knowledge
#Copyright (C) 2011  Daniel Guerrero Martinez & Sergio Rodriguez Lumley
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.contrib.auth.models import User
from django.contrib.auth.forms import SetPasswordForm, PasswordChangeForm
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, HttpResponseRedirect
from django.http import Http404, HttpResponse
from django.template import RequestContext
from forms import UserChange,RemoveUser
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.contrib.auth import logout

    
@login_required
def profile(request):

    formPassword = PasswordChangeForm(request.user)
    formUser = UserChange()
    formRemove = RemoveUser()
    
    if request.method == "POST":
        type = request.POST['type']
        if type == 'user':
            formUser = UserChange(request.POST,instance=request.user)
            if formUser.is_valid() :
                formUser.save()
                messages.success(request, _('Your nicknane has been changed'))
        elif type == 'isContributor':
            if "isCollaborator" in request.POST:
                request.user.visibleContributor = True
                messages.success(request, _('You now appear as a contributor'))
            else:
                request.user.visibleContributor = False
                messages.success(request, _('You do not appear as a contributor anymore'))
            request.user.save()
        elif type == 'removeUser':
            formRemove = RemoveUser(request.POST,instance=request.user)
            if formRemove.is_valid():
                formRemove.save()
                request.user.set_unusable_password()
                logout(request)
                return HttpResponseRedirect("/")
        
        else:
            messages.warning(request, _('Unknown request, there has been no change'))

    contex = RequestContext(request, { 'user' : request.user,
                                       'passwordChange': formPassword,
                                       'userChange' : formUser,
                                       'removeUser': formRemove })
    
    return render_to_response('userManager/profile.html', contex)
    
    
    










